#include "qcircularwidget.h"

#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"


#include <QPainter>
#include <QGradient>


QCircularWidget::QCircularWidget(QWidget *parent) :
    QAbstractGraphicsWidget(parent)
{
}

void QCircularWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing,m_antialiasing);
    p.setPen(m_pen);
    QGradient g=property("fill_color").value<QGradient>();
    p.setBrush(QBrush(g));
    QRect re=this->rect();
    p.drawEllipse(re.adjusted((m_pen_info.m_width+1)/2,
                              (m_pen_info.m_width+1)/2,
                              -1-m_pen_info.m_width/2,
                              -1-m_pen_info.m_width/2));
}


QCircularWidgetHost::QCircularWidgetHost(QAbstractHost *parent):
    QAbstractGraphicsHost(parent)
{

}

void QCircularWidgetHost::create_object()
{
    m_object=new QCircularWidget;
    m_object->setObjectName("circularWidget");
}

QString QCircularWidgetHost::get_show_name()
{
    return tr("Circular");
}

QString QCircularWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QCircularWidgetHost::get_show_icon()
{
    return ":/base_grahpics_images/circular.png";
}

void QCircularWidgetHost::init_property()
{
    QAbstractGraphicsHost::init_property();

    QAbstractProperty* pro;

    pro=get_property("pen");
    if(pro!=NULL)
    {
        pro->set_attribute("pen_join_style:Visible",false);
    }

    QCircularWidget* wid=(QCircularWidget*)m_object;

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","fill_color");
        pro->set_attribute("show_name",tr("Fill Color"));
        pro->set_attribute("group","Attributes");
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        g.setColorAt(1,QColor(0,0,0,0));
        m_object->setProperty("fill_color",QVariant::fromValue<QGradient>(g));
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

qRegisterHost("basegraphics.circular",QCircularWidgetHost,5040)
